#!/usr/bin/env python
"""
# -*- coding: utf-8 -*-
# @Time    : 2020/9/21 22:03
# @Author  : AshJo
# @Desc    : 写入与读取对象
# @Filename: pickle_demo.py
# @Version : v1.0
"""
import pickle


class Cource:
    def __init__(self, name, period, price):
        self.name = name
        self.period = period
        self.price = price


# 创建三个对象
python = Cource('python', 6, 21800)
linux = Cource('linux', 5, 19800)
go = Cource('go', 4, 12800)

# pickle保存写入创建的对象
with open('pickle_file', 'wb') as f:
    pickle.dump(python, f)
    pickle.dump(linux, f)
    pickle.dump(go, f)

# 从文件中读取对象的数据
with open('pickle_file', 'rb') as f:
    while True:
        try:
            obj = pickle.load(f)
            print(obj.name, obj.period, obj.price)
        except EOFError:
            break
